package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.cyz.dao.impl.ProducttypeDaoImpl;
import com.cyz.pojo.Producttype;

import com.cyz.service.ProductService;
import com.cyz.service.impl.ProductServiceImpl;
import com.google.gson.Gson;

@Controller
@RequestMapping("/CategoryManage")
public class CategoryManage {
	
	@RequestMapping("/category")
	public ModelAndView categoryManage(HttpServletRequest request,HttpServletResponse response) throws SQLException {
		//ApplicationContext ctx = new ClassPathXmlApplicationContext("com/cxf/pojo/applicationContext.xml");
		Gson gson = new Gson();
		ProductService productService = new ProductServiceImpl();
		ModelAndView mv = new ModelAndView();		
		ProducttypeDaoImpl typeService = new ProducttypeDaoImpl();
		List<Producttype> typeList = typeService.getAllProducttype();
		String s = gson.toJson(typeList);
		mv.addObject("typeList",s);
		mv.setViewName("admin/category");
		return mv;
	}
	@RequestMapping("/category-add")
	public ModelAndView categoryAdd(HttpServletRequest request,HttpServletResponse response) {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("admin/category-add");
		return mv;
	}
	
	/**
	 * 添加种类
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/addType")
	public void addType(HttpServletRequest request,HttpServletResponse response) throws IOException {		
		PrintWriter printWriter = response.getWriter();
		ProductService productService = new ProductServiceImpl();
		String typeName = request.getParameter("typeName");
		System.out.println(typeName);
		try {
			productService.addType(typeName);
			printWriter.write("{\"res\":\"1\"}");
		} catch (Exception e) {
			printWriter.write("{\"res\":\"0\"}");
			//e.printStackTrace();
		}
		printWriter.close();
	}

	/**
	 * 删除一级分类
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/delete")
	public void delete(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		ProductService productService = new ProductServiceImpl();

			String typeName = request.getParameter("typeName");
			productService.deleteType(typeName);
			printWriter.write("{\"res\":\"1\"}");
		
		printWriter.close();
	}
	/**
	 * 更新分类名称
	 * @param
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping("/update")
	public void update(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		ProductService productService = new ProductServiceImpl();
			Map map = new HashMap<Object,Object>();
			map.put("typeName",request.getParameter("typeName"));
			System.out.println(request.getParameter("typeName"));
			map.put("newName",request.getParameter("newName"));
			System.out.println(request.getParameter("newName"));
			map.put("typeId",productService.getTypeIdByTypeName(request.getParameter("typeName")));
			try {
				productService.renameType(map);
				//重命名未实现
				printWriter.write("{\"res\":\"1\"}");
			} catch (Exception e) {
				printWriter.write("{\"res\":\"0\"}");
			}
		printWriter.close();
	}
	
}
